package com.company.project.core.ocr;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.company.project.core.common.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * =================================
 * <p>
 * Created by CJNusun on 2021/6/4.
 * <p>
 * 描述: 百度OCR识别服务
 */

@Slf4j
public class BdOCRRecService implements IOCRService {

    public static final String ORC_URL = "http://localhost:9997/api/document/ocr";

    @Override
    public String recStr(String url, String filePath) throws Exception {
        return rec(url, filePath).getWhole_text();
    }

    @Override
    public RotatedImageDTO rec(String url, String filePath) throws Exception {
        Map<String, String> params = new HashMap<>();
        String type = filePath.substring(filePath.lastIndexOf('.') + 1);
        params.put("type", type);
        String rec = HttpClientUtils.uploadYunyiDocFile(url, filePath, params);
        Map<String, Object> map = JSON.parseObject(rec, Map.class);
        Integer code = (Integer) map.get("code");
        if (code != null && code == 200) {
            List<JSONObject> pvResponses = JSONObject.toJavaObject((JSON) map.get("result"), List.class);
            return convert(pvResponses);
        } else {
            Object errorMsg = map.get("message");
            if (errorMsg != null) {
                throw new Exception((String) errorMsg);
            }
            throw new Exception("OCR服务接口异常!");
        }
    }

    private static RotatedImageDTO convert(List<JSONObject> pvResponses) {
        RotatedImageDTO rotatedImageDTO = new RotatedImageDTO();
        StringBuilder accum = new StringBuilder();
        List<LineImageDTO> lines = new ArrayList<>(pvResponses.size());
        for (JSONObject jsonObject : pvResponses) {
            LineImageDTO lineImageDTO = new LineImageDTO();
            String result = jsonObject.getString("result");
            lineImageDTO.setText(result);
            List<Integer> position = new ArrayList<>();
            position.add(jsonObject.getInteger("x1"));
            position.add(jsonObject.getInteger("y1"));
            position.add(jsonObject.getInteger("x2"));
            position.add(jsonObject.getInteger("y2"));
            position.add(jsonObject.getInteger("x3"));
            position.add(jsonObject.getInteger("y3"));
            position.add(jsonObject.getInteger("x4"));
            position.add(jsonObject.getInteger("y4"));
            lineImageDTO.setPosition(position);
            List<int[]> charPolygons = new ArrayList<>();
            int[] arr = position.stream()
                    .mapToInt(Integer::intValue)
                    .toArray();
            charPolygons.add(arr);
            lineImageDTO.setChar_polygons(charPolygons);
            lines.add(lineImageDTO);
            accum.append(result + "\n");
        }
        rotatedImageDTO.setWhole_text(accum.substring(0, accum.lastIndexOf("\n")));
        rotatedImageDTO.setLines(lines);
        return rotatedImageDTO;
    }

}
